<?php
    header("Content-Type:text/html; charset=utf-8");
    if (!defined("APP_DEBUG")) 
    {
        define('APP_DEBUG',TRUE); //开启调试模式
    }
    
    //定义WEB的标题
    if (!defined("HOME_TITLE")) 
    {
        define('HOME_TITLE', "10网络技术");
    }

    //定义SESSION的登录
    if (!defined('SESSION_ISLOGIN')) 
    {
        define('SESSION_ISLOGIN', "wlIsLogin");         
    }

    //显示内容的数量
    if (!defined("PAGE_NUMBER")) 
    {
        define("PAGE_NUMBER", "20");    
    }
    
    //图片前缀.
    if (!defined("PREFIX_PHOTO"))
    {
        define("PREFIX_PHOTO", "m_,s_");        
    }

    // 邮箱验证地址
    if (!defined("WEB_SITE_PATH"))
    {
        define("WEB_SITE_PATH", "http://localhost:8080/appid3ad5e1exkb/wangluo/index.php");    
    }

    //时间函数处理
    function get_mydate($mydate){
        @$newdate=explode(" ",$mydate);
        return $newdate[0];
    }
    //错误记录函数
    function writelog($content,$error){
        $path=$_SERVER["DOCUMENT_ROOT"]."/wangluo/wangluo/log/";
        $fname=$path.date("Ymd",time()).".txt";
        $fop=fopen($fname,"a+");
        if($fop){
            date_default_timezone_set('PRC');
            $input=date("H-i-s")."\t".$content."\t"."错误：".$error."\r\n";
            fwrite($fop,$input);
            fclose($fop);
        }
    }
   function add($arr,$table){
        global $pdo;
        $valuestr="";
        $keystr="";
        foreach($arr  as $value=>$key){
            $valeustr.=$value.",";
            $keystr.="'".$key."',";
        }
        $valeustr=rtrim($valeustr,",");
        $keystr=rtrim($keystr,",");
        $sql=" insert into ".$table."(".$valeustr.")"."values(".$keystr.")";
    return ($sql);
    }
    //转义
    function zhunyi($json){
        foreach($json as $key=>$value){
            if(!get_magic_quotes_gpc()){
              $value = "'".addslashes($value)."'";
            }else{
              $value= $value;
            }
      }
      return $json;
    }
    //创建相册的文件夹
    function createFolder($path){  
       if (!file_exists($path)){
          createFolder(dirname($path));   
          mkdir($path, 0777);  
       } 
    }
    //转编码
    function mb_utf8($file)
    {
        $mbString = mb_detect_encoding($file, array('ASCII','UTF-8','GB2312','GBK','BIG5')); 
        
        // if ($mbString != "UTF-8")
        // { 
            $file = mb_convert_encoding($file, $mbString, "GB2312"); 
        // }

        return $file;
    } 

    //匹配汉字方法
    function hanzi($str){
        if (preg_match("/[\x{4e00}-\x{9fa5}]/u",$str)){
           return true;
        }
        return false;
    }
    
    /**
+----------------------------------------------------------
* 字符串截取，支持中文和其他编码
+----------------------------------------------------------
* @static
* @access public
+----------------------------------------------------------
* @param string $str 需要转换的字符串
* @param string $start 开始位置
* @param string $length 截取长度
* @param string $charset 编码格式
* @param string $suffix 截断显示字符
+----------------------------------------------------------
* @return string
+----------------------------------------------------------
*/
function msubstr($str, $start=0, $length, $charset="utf-8", $suffix=true)
{
    if(function_exists("mb_substr"))
    {
       if($suffix)
       {
            return mb_substr($str, $start, $length, $charset)."...";
       }
       else
       {
            return mb_substr($str, $start, $length, $charset);
        }
    }
    elseif(function_exists('iconv_substr'))
    {
        if($suffix)
        {
                return iconv_substr($str,$start,$length,$charset)."...";
        }
        else
        {
            return iconv_substr($str,$start,$length,$charset);
        }
    }
    $re['utf-8']   = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
    $re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
    $re['gbk']    = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
    $re['big5']   = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
    preg_match_all($re[$charset], $str, $match);
    $slice = join("",array_slice($match[0], $start, $length));

    if($suffix)
    { 
         return $slice."...";
    }
    else
    {
       return $slice;
    }
}
/*
* 打印函数.
*/
function ep($str)
{
    echo "<pre>";
    print_r($str);
    echo "</pre>";
    exit();
}

/**
 * 性别转换.
 * @param int $sex 性别d
 * @return string
 */
function getSex($sex = 1)
{
    $sexContent = '';
    
    switch ($sex) {
        case '1':
            $sexContent = "男";
            break;
        case '2':
            $sexContent = "女";
        default:
            $sexContent = $sex;
            break;
    }

    return $sexContent;
}

/**
 * 随机生成五位数字
 */
function rand5Num()
{
    $str = '0123456789';

    //随机打乱字符串
    $strShuffle = str_shuffle($str);

    return substr($strShuffle , 0, 5);
}

/**
 * 把YYYY-MM-DD时间格式转为时间戳
 */
function date2time($timeDate)
{
    if (empty($timeDate))
    {
        $timeDate = date('Y-m-d');
    }

    list($y, $m, $d) = explode('-', $timeDate);

    return mktime('0', '0', '0', $m, $d, $y);
}

?>